package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.api.CommonFileApi;
import com.common.core.domain.entity.SysUser;
import com.common.core.domain.model.LoginUser;
import com.common.utils.SecurityUtils;
import com.common.utils.StringUtils;
import com.domain.entity.BizFileShare;
import com.mapper.FileSharedMapper;
import com.service.IFileSharedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class FileSharedServiceImpl extends ServiceImpl<FileSharedMapper, BizFileShare>   implements IFileSharedService {
    @Autowired
    private FileSharedMapper baseMapper;
    //文件上传
    @Autowired
    private CommonFileApi fileApi;
    @Override
    public Boolean addFileShared(BizFileShare bizFileShare) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        bizFileShare.setUploadPersonId(user.getUserRealId());
        return this.save(bizFileShare);
    }

    @Override
    public void delFileShared(Collection ids) {
        LambdaQueryWrapper<BizFileShare> lqw=new LambdaQueryWrapper<>();
        lqw.in((ids != null && ids.size() !=0),BizFileShare::getId,ids);
        List<BizFileShare> fileSharedList = this.list(lqw);
        fileSharedList.forEach(e->{
            boolean delBase = this.removeById(e.getId());
            if (delBase){
                //删除本地文件
                try {
                    fileApi.delFile(e.getFileId());
                } catch (Exception ex) {
                    log.error("共享文档模块删除文件失败,文件id:"+e.getFileId());
                }
            }
        });
    }

    @Override
    public Boolean updateFileShared(BizFileShare bizFileShare) {
        BizFileShare old = this.getById(bizFileShare.getId());
        boolean update = this.updateById(bizFileShare);
        if (update){
            //删除本地文件
            try {
                fileApi.delFile(old.getFileId());
            } catch (Exception e) {
                log.error("共享文档模块删除文件失败");
            }
        }
        return update;
    }

    @Override
    public List<BizFileShare> selectFileShared(BizFileShare bizFileShare) {
       QueryWrapper<BizFileShare> lqw=new QueryWrapper<>();
       lqw.like(StringUtils.isNotEmpty(bizFileShare.getFileName()),"up.UPLOADFILENAME", bizFileShare.getFileName());
       lqw.eq(StringUtils.isNotEmpty(bizFileShare.getCategoryId()),"category_id", bizFileShare.getCategoryId());
       lqw.like(StringUtils.isNotEmpty(bizFileShare.getUploadPersonName()),"su.person_name", bizFileShare.getUploadPersonName());
       lqw.orderBy(true,false,"up.LASTEDITDATE");
        List<BizFileShare> fileShares = baseMapper.selectFileShared(lqw);
        return fileShares;
    }
}
